package com.itcncp.common.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.HashMap;
import java.util.Map;

public class Jpush {


	/***********************************极光配置信息***********************************/

	public static final String JPUSH_APPKEY = "e584379525523df263cc43d2";

	public static final String JPUSH_MASTER_SECRET = "91dcb568db1d20ab01a23743";


	/***********************************极光管理员配置信息***********************************/

	public static final String JPUSH_ADMIN_APPKEY = "305366ef7496099e7d7985b8";

	public static final String JPUSH_ADMIN_MASTER_SECRET = "a60ab9496f3f633a95a49df0";



	public static JPushClient jpushClient = new JPushClient(JPUSH_MASTER_SECRET, JPUSH_APPKEY);

	/**
	 * 推送通知给所有app的所有设备 
	 * @param content 内容
	 */
	public static PushResult allDevices(String content, Map<String, String> extras){

		HashMap<String,Object> response = new HashMap<String,Object>();

		PushPayload pushPayload = PushPayload.newBuilder()
		        .setPlatform(Platform.android_ios()) 
		        .setAudience(Audience.all()) 
		        .setNotification(Notification.newBuilder() 
		                .addPlatformNotification(IosNotification.newBuilder()
		                        .setAlert(content)
		                        .setBadge(1)
		                        .setSound("happy.caf")  
		               		 	.addExtras(extras)
		                        .setContentAvailable(false) //推送的时候携带"content-available":true 说明是 Background Remote Notification，如果不携带此字段则是普通的Remote Notification。
		                        .build())
		                .addPlatformNotification(AndroidNotification.newBuilder()
		                		.setAlert(content) 
		                		.setBuilderId(3)  
		               		 	.addExtras(extras)
		                		.build())
		                .build())  
		         .setMessage(Message.newBuilder()
		        		 .setMsgContent(content) 
		        		 .setContentType("text")
		        		 .addExtras(extras) 
		        		 .build())
		         .setOptions(Options.newBuilder()
		                 .setApnsProduction(true)
		                 .build())
		         .build(); 

		PushResult pushResult = new PushResult();
		try {
			pushResult = jpushClient.sendPush(pushPayload);
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return pushResult;
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return pushResult;
		}
		return pushResult;
	}
	
	public static PushResult notifiAccount(String username, String content){
		
		PushPayload pushPayload = PushPayload.newBuilder()
		        .setPlatform(Platform.android_ios())
		        .setAudience(Audience.alias(username))    
		        .setNotification(Notification.newBuilder()
		                .addPlatformNotification(IosNotification.newBuilder()
		                        .setAlert(content)
		                        .setBadge(1)
		                        .setSound("happy.caf") 
		                        .setContentAvailable(true) //推送的时候携带"content-available":true 说明是 Background Remote Notification，如果不携带此字段则是普通的Remote Notification。
		                        .build())
		                .addPlatformNotification(AndroidNotification.newBuilder()
		                		.setAlert(content)
		                		.setBuilderId(3) 
		                		.build())
		                .build()) 
		         .setOptions(Options.newBuilder()
		                 .setApnsProduction(true)
		                 .build())
		         .build(); 

		
		PushResult pushResult = new PushResult();
		try {
			pushResult = jpushClient.sendPush(pushPayload);
		} catch (APIConnectionException e) { 
			e.printStackTrace();
			return pushResult;
		} catch (APIRequestException e) { 
			e.printStackTrace();
			return pushResult;
		}

		return pushResult;
		
	}
	
public static PushResult notifiAccount(String username, String content, String title){
		
		PushPayload pushPayload = PushPayload.newBuilder()
		        .setPlatform(Platform.android_ios())
		        .setAudience(Audience.alias(username))    
		        .setNotification(Notification.newBuilder()
		                .addPlatformNotification(IosNotification.newBuilder()
		                        .setAlert(content)
		                        .setBadge(1)
		                        .setSound("happy.caf") 
		                        .setContentAvailable(true) //推送的时候携带"content-available":true 说明是 Background Remote Notification，如果不携带此字段则是普通的Remote Notification。
		                        .build())
		                .addPlatformNotification(AndroidNotification.newBuilder()
		                		.setAlert(content)
		                		.setBuilderId(3) 
		                		.build())
		                .build())
		         .setMessage(Message.newBuilder()
		        		 .setMsgContent(content)
		        		 .setTitle(title)
		        		 .setContentType("text") 
		        		 .build())
		         .setOptions(Options.newBuilder()
		                 .setApnsProduction(true)
		                 .build())
		         .build(); 

		
		PushResult pushResult = new PushResult();
		try {
			pushResult = jpushClient.sendPush(pushPayload);
		} catch (APIConnectionException e) { 
			e.printStackTrace();
			return pushResult;
		} catch (APIRequestException e) { 
			e.printStackTrace();
			return pushResult;
		}

			return pushResult;
		
	}
	   
	/**
	 * 推送通知给单个账户别名
	 * @param username 用户名
	 * @param content 内容
	 * @param extra 自定义内容 
	 */
	public static PushResult notifiAccount(String username, String content,Map<String, String> extras){

		HashMap<String,Object> response = new HashMap<String,Object>();

		PushPayload pushPayload = PushPayload.newBuilder() 
        .setPlatform(Platform.android_ios())
        .setAudience(Audience.alias(username))  
        .setNotification(Notification.newBuilder() 
                .addPlatformNotification(IosNotification.newBuilder()
                        .setAlert(content)
                        .setBadge(1)
                        .setSound("happy.caf")  
               		 	.addExtras(extras)
                        .setContentAvailable(false) //推送的时候携带"content-available":true 说明是 Background Remote Notification，如果不携带此字段则是普通的Remote Notification。
                        .build())
                .addPlatformNotification(AndroidNotification.newBuilder()
                		.setAlert(content) 
                		.setBuilderId(3)  
               		 	.addExtras(extras)
                		.build())
                .build())  
//         .setMessage(Message.newBuilder()
//        		 .setMsgContent(content)
//        		 .setContentType("text")
//        		 .addExtras(extras)
//        		 .build())
         .setOptions(Options.newBuilder()
                 .setApnsProduction(true)
                 .build())
         .build();

		PushResult pushResult = new PushResult();
		
		try {
			pushResult = jpushClient.sendPush(pushPayload);
		} catch (APIConnectionException e) { 
			e.printStackTrace();
			return pushResult;
		} catch (APIRequestException e) { 
			e.printStackTrace();
			return pushResult;
		}

		return pushResult;
	}  
	

	/**
	 * 推送透传给单个账户别名
	 * @param username 用户名
	 * @param content 内容
	 * @param extra 自定义内容 
	 */
	public static PushResult messageAccount(String username, String content, Map<String, String> extras){

		HashMap<String,Object> response = new HashMap<String,Object>();
		PushPayload pushPayload = PushPayload.newBuilder()
	        .setPlatform(Platform.android_ios())
	        .setAudience(Audience.alias(username))
	        .setMessage(Message.newBuilder()
        			.setMsgContent(content)
        			.addExtras(extras)
        			.build())
        	.setOptions(Options.newBuilder()
        		 	.setApnsProduction(true)
        		 	.build())
         .build(); 
		 
		
		PushResult pushResult = new PushResult();
		
		try {
			pushResult = jpushClient.sendPush(pushPayload);
		} catch (APIConnectionException e) { 
			e.printStackTrace();
			return pushResult;
		} catch (APIRequestException e) { 
			e.printStackTrace();
			return pushResult;
		}

		return pushResult;
	}  
	

	/**
	 * 推送透传给单个账户别名
	 * @param username 用户名
	 * @param content 内容
	 * @param extra 自定义内容 
	 */
	public static PushResult  messageAccount(String username, String content){

	    HashMap<String,Object> response = new HashMap<String,Object>();

		PushPayload pushPayload = PushPayload.newBuilder()
	        .setPlatform(Platform.android_ios())
	        .setAudience(Audience.alias(username))
	        .setMessage(Message.newBuilder()
        			.setMsgContent(content) 
        			.build())
        	.setOptions(Options.newBuilder()
        		 	.setApnsProduction(true)
        		 	.build())
         .build(); 
		  
		PushResult pushResult = new PushResult();
		
		try {
			pushResult = jpushClient.sendPush(pushPayload);
		} catch (APIConnectionException e) { 
			e.printStackTrace();
			return pushResult;
		} catch (APIRequestException e) { 
			e.printStackTrace();
			return pushResult;
		}
		return pushResult;
	}  
	
}